package org.dark.fund.web;

import org.dark.fund.api.dto.UserAddDTO;
import org.dark.fund.api.dto.UserDeleteDTO;
import org.dark.fund.common.ApiResult;
import org.dark.fund.dao.dataobject.UserBaseInfo;
import org.dark.fund.web.request.UserAddRequest;
import org.dark.fund.web.request.UserDeleteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import org.dark.fund.api.UserService;

import javax.validation.Valid;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Component
@RestController
@RequestMapping("/user")
public class UserController {

    private static final BeanCopier addCopier = BeanCopier.create(UserAddRequest.class, UserAddDTO.class, false);
    private static final BeanCopier deleteCopier = BeanCopier.create(UserDeleteRequest.class, UserDeleteDTO.class, false);

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public ApiResult<UserBaseInfo> getUserBaseInfo(@PathVariable(value = "id") String id) {
        return ApiResult.ok(userService.getUserBaseInfo(id));
    }

    @PostMapping("/register")
    public ApiResult<Boolean> addUser(@Valid @RequestBody UserAddRequest addRequest) {
        UserAddDTO userAddDTO = new UserAddDTO();
        addCopier.copy(addRequest, userAddDTO, null);
        return ApiResult.ok(userService.registerUser(userAddDTO));
    }

    @PostMapping("/unregister")
    public ApiResult<Boolean> deleteUser(@Valid @RequestBody UserDeleteRequest deleteRequest) {
        UserDeleteDTO userDeleteDTO = new UserDeleteDTO();
        deleteCopier.copy(deleteRequest, userDeleteDTO, null);
        return ApiResult.ok(userService.unregisterUser(userDeleteDTO));
    }


}
